<?php
/*

Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
include_once($semf_root.'page_designer/components/Component.php');

class TableComponent extends Component
{
	private static $WIDTH_PREFIX = "Width-Column";
	
	/** properties with default values */
	public static function get_properties(){
		return array( 'Apearance' => array(
					 	'Columns'=> 2),
					 'DataBinding' => array()
						
					 );
	}
	
	/** This happens when container changes.. */
	public static function adjust_properties_with_container($parent_container, $properties)
	{
		// initally $key == NULL, 	
		$component = $parent_container->firstChild;
		
		// here we need to think only apearance properties
		$apearance_properties = $properties['Apearance'];
		
		$parent_width = $parent_container-> getAttribute('width');
		
		$cols =$properties['Apearance']['Columns'];
		
		
		$col_sum = 0;
		
		for( $i = 0; $i < $cols; $i ++ )
		{
			$column_id = TableComponent::$WIDTH_PREFIX.$i;
			$width = $properties['Apearance'][$column_id];
			if ( $width == NULL ) //devide things evenly..
			{
				// this should happen only when at the start..
				return NULL;
			}
			$col_sum += $width;
		}
		
		// this happens when parent had shrinked or widened a little bit..
		if ( $col_sum != 0 && 
				( ($col_sum - $parent_width) > 0.5 ||
				  ($col_sum - $parent_width) < -0.5) )
		{
			$ratio = $parent_width / $col_sum;
			$col_sum = 0;
		
			for( $i = 0; $i < $cols; $i ++ )
			{
				$column_id = TableComponent::$WIDTH_PREFIX.$i;
				$width = $properties['Apearance'][$column_id] * $ratio;
				if ( $i == $cols -1  ) //devide things evenly..
				{
					$width = $parent_width - $col_sum;
				}
				$properties['Apearance'][$column_id] = $width;
				$col_sum += $width;
			}
		}
		return $properties;
	}
	
	
	/** build properties dynamically if atmost one property is changed */
	public static function build_properties($parent_container, $properties, $property_group, $key, $value)
	{
		// initally $key == NULL, 	
		$component = $parent_container->firstChild;
		
		/** here we need to think only apearance properties */
		$apearance_properties = $properties['Apearance'];
				
		$parent_width = $parent_container-> getAttribute('width');
		
		$cols = $apearance_properties['Columns'];
		
		$row_sum = 0;
		$col_sum = 0;
		/** Key == NULL 
		 *   this is assumed is at the start..
		 *   when user drag and drop an item
		 *   When open an existing file, so all the components have to be loaeded.
		 */
		if ( $key == NULL )
		{	
			for( $i = 0; $i < $cols; $i ++ )
			{
				$column_id = TableComponent::$WIDTH_PREFIX.$i;
				$width = $apearance_properties[$column_id];
				if ( $width == NULL ) //devide things evenly..
				{
					$width = ($parent_width - $col_sum)/($cols - $i );
					$apearance_properties[$column_id] = $width;
				}
				$col_sum += $width;
			}
		}
		else if ( $key == 'Columns')
		{
			$col_sum = 0;
			if ( $value < $cols ) // when new value is less
			{
				for( $i = 0; $i < $value; $i ++ )
				{
					$column_id = TableComponent::$WIDTH_PREFIX.$i;
					$width = $apearance_properties[$column_id];
					$col_sum +=  $width;
				}
				if ($col_sum != 0 )
				{
					$ratio = $parent_width / $col_sum;
					
					$col_sum = 0;
					
					for( $i = 0; $i < $value; $i ++ )
					{
						$column_id = TableComponent::$WIDTH_PREFIX.$i;
						$width = $apearance_properties[$column_id]* $ratio;
						if ( $i == value - 1) // correct the round off error
						{
							$width = $parent_width - $col_sum;
						}
						$col_sum +=  $width;
						$apearance_properties[$column_id] = $width;
					}
				}
			}
			else if ( $value > $cols ) // when new value is more
			{
				$new_cols_width = $parent_width / $value;
				
				$old_cols_width_total = $parent_width - $new_cols_width * ( $value- $cols );
				
				if( $parent_width != 0 )
				{
					$ratio = $old_cols_width_total / $parent_width;
					for( $i = 0; $i < $value; $i ++ )
					{
						$column_id = TableComponent::$WIDTH_PREFIX.$i;
						$width = $apearance_properties[$column_id]* $ratio;
						$col_sum +=  $width;
						$apearance_properties[$column_id] = $width;
					}
				}
				
				//update the $new_cols_width covering the round off error..
				$new_cols_width = ($parent_width - $col_sum ) / ($value- $cols );
				for ( $i = $cols; $i < $value ; $i ++ )
				{
					$column_id = TableComponent::$WIDTH_PREFIX.$i;
					$apearance_properties[$column_id] = $new_cols_width;
				}
				
			}
			$cols = $apearance_properties['Columns'] = $value;
		}

		else //change in widths or height
		{
			//change of a width
			$change_width = false;
			$column_sum = 0;
			$ratio = 0;
			for( $i = 0; $i < $cols; $i ++ )
			{
				$column_id = TableComponent::$WIDTH_PREFIX.$i;
				if ( $column_id == $key ) // this is the value to change
				{
					$old_value = $apearance_properties[$column_id];
					if( $column_sum + $value > $parent_width ) // too much width
					{
						break;
					}
					$ratio = ($parent_width - ( $column_sum + $value ) )/
					           ($parent_width - ( $column_sum + $old_value ) );
					$change_width = true;
					$apearance_properties[$column_id] = $value;
				}
				else if( $change_width == true )
				{
					if( $i == $cols -1 ) //avoid the round off error..
					{
						$apearance_properties[$column_id] = $parent_width - $column_sum;
					}
					else
					{
					 	$apearance_properties[$column_id] *= $ratio;
					}
				}
				
				$column_sum += $apearance_properties[$column_id];
			}
			
						
		}
		

		$new_apearance_properties = array(
					 				'Columns'=> $apearance_properties['Columns']);
					 
		
		for( $i = 0; $i < $cols; $i ++ )
		{
			$column_id = TableComponent::$WIDTH_PREFIX.$i;
			$temp = array("$column_id"=> $apearance_properties[$column_id]);
			
			$new_apearance_properties = array_merge ($new_apearance_properties, $temp);
		}
				
				
		$new_array['Apearance'] = $new_apearance_properties;
		
		/** now just copy the rest tabs to the new array */
		$property_tab_keys = array_keys($properties);
		foreach( $property_tab_keys as $property_tab_key )
		{
			if( $property_tab_key == 'Apearance' ||   // these both will be updated in difference way
			   $property_tab_key == 'DataBinding' )
			{
				continue;
			}
			$new_array[$property_tab_key] = $properties[$property_tab_key];
		}
		// at the end fill every column with some thing
		
		$cols = $new_array["Apearance"]["Columns"];
		for( $i = 0; $i < $cols; $i ++ )
		{
			if( $key == "Name-Column".$i && $property_group == "Apearance" )
			{
				$new_array["Apearance"]["Name-Column".$i] = $value;
			}
			else if( isset($properties["Apearance"]["Name-Column".$i]))
			{
				$new_array["Apearance"]["Name-Column".$i] = $properties["Apearance"]["Name-Column".$i];
			}
			else
			{
				$new_array["Apearance"]["Name-Column".$i] = "Column".$i;
			}
			
			if( $key == "Value-Column".$i && $property_group == "DataBinding" )
			{
				$new_array["DataBinding"]["Value-Column".$i] = $value;
			}
			else if( isset($properties["DataBinding"]["Value-Column".$i]))
			{
				$new_array["DataBinding"]["Value-Column".$i] = $properties["DataBinding"]["Value-Column".$i];
			}
			else
			{
				$new_array["DataBinding"]["Value-Column".$i] = "";
			}
		}
		
		return $new_array;
		
	}
	
	/** containers */
	public static function build_containers( $parent_container, $properties)
	{
		//we do not have any container for table ...
		return array( );
	}
	
	/** get the property info to use in databinding */
	public static function get_property_info($component_id, $properties)
	{
		$info_array = array();
		$keys = array_keys($properties);
		foreach($keys as $key)
		{
			$temp_array = array($key => array(
										'attri' => 'innerHTML', //that is to get elem1.value 
										'databind'=> $component_id."_".$key));
			$info_array = array_merge($info_array, $temp_array);
		}
		return $info_array;
	}
}
?>